package com.oazon.voicelib.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import com.moco.launcher.R;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.ui.SpechEventListener;
import com.oazon.voicelib.model.SemanticObj;
import com.oazon.voicelib.service.SpeechServer.MyBind;

public class SpeechManage {
	
	private boolean isWake = false;
	private static SpeechManage speechManage = null;

	private SpeechServer speechServer = null;

	public synchronized static SpeechManage getInstance() {
		if (speechManage == null) {
			speechManage = new SpeechManage();
		}
		return speechManage;
	}

	private static Context context = null;

	public static void init(Context context) {
		SpeechManage.context = context;
	}

	public void destroy() {
		context.unbindService(speechServiceConnection);
	}

	private SpeechManage() {
		Intent speechService = new Intent(context, SpeechServer.class);
		context.bindService(speechService, speechServiceConnection,
				Context.BIND_AUTO_CREATE);
		
		LinkSDK.getInstance().registLinkListener(spechEventListener);
	}

	ServiceConnection speechServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName arg0) {

		}

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			if(arg1 instanceof MyBind){
				speechServer = ((MyBind) arg1).getService();
				speechServer.registTalker(talker);
				speechServer.registWakeuperListener(onWakeuperListener);	
			}
		}
	};

	public void stopWake() {
		speechServer.stopWake();
	}
	
	public void startWake(){
		speechServer.startWake();
	}
	OnWakeuperListener onWakeuperListener = new OnWakeuperListener() {
		
		@Override
		public void onWakeuper() {
			isWake = true;
		}
	};	
	
	SpechEventListener spechEventListener = new SpechEventListener() {

		@Override
		protected void speechStop() {
			speechServer.stopUnderstand();
		}

		@Override
		protected void speechStart() {
			speechServer.startUnderstand();
			MediaPlayer mp = MediaPlayer.create(SpeechManage.context , R.raw.standby);
			Log.v("SpeechManage","==="+isWake);
			if (isWake){
				mp.start();
				isWake = false;
			}
		}

		@Override
		protected void speechStopSpeak() {
			speechServer.stopSeechSynthesizer();
		}

		@Override
		protected void speechStartSpeak(String string) {
			speechServer.startSpeechSynthesizer(string);
		}

		@Override
		protected void speechSpeakComplite(boolean isWake) {
			if(isWake){
				LinkSDK.getInstance().speechStart(LinkEventController.speakEventSource);
				isWake = false;
			}
		}

	
	};

	Talker talker = new Talker() {

		@Override
		public void talkVolume(int volume) {
			LinkSDK.getInstance().speechVolume(
					LinkEventController.speakEventSource, volume);
		}

		@Override
		public void talkUnderstandRs(String string) {
			LinkSDK.getInstance().speechUnderstandRs(
					LinkEventController.speakEventSource, string);

		}

		@Override
		public void talkUnderstandRs(SemanticObj result) {
			LinkSDK.getInstance().speechUnderstandRs(
					LinkEventController.speakEventSource, result);
		}

		@Override
		public void onUnderstandError(String error) {
			LinkSDK.getInstance().speechUnderstandError(
					LinkEventController.speakEventSource, error);

		}

		@Override
		public void endTts(boolean isWake) {
			LinkSDK.getInstance().speechSpeakComplite(
					LinkEventController.speakEventSource,isWake);

		}

		@Override
		public void wakeUnderstand() {
			LinkSDK.getInstance().speechStart(LinkEventController.speakEventSource);
			
		}

		@Override
		public void onEndOfUnderstander() {
			LinkSDK.getInstance().speechEnd(
					LinkEventController.speakEventSource);
			
		}
	};
}
